package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.OrderRefund;
import com.caocaog.market.service.IOrderRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 订单退款信息
 *
 * @author jeecg-boot
 * @date 2021-12-01
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(value = "orderRefund", tags="订单退款信息")
@RequestMapping("/order/orderRefund")
public class OrderRefundController extends JeecgController<OrderRefund, IOrderRefundService> {
	@Autowired
	private IOrderRefundService orderRefundService;
	@Autowired
	private CommonAPI commonAPI;

	/**
	 * 分页列表查询
	 *
	 * @param orderRefund
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "订单退款信息-分页列表查询")
	@ApiOperation(value="订单退款信息-分页列表查询", notes="订单退款信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OrderRefund orderRefund,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OrderRefund> queryWrapper = QueryGenerator.initQueryWrapper(orderRefund, req.getParameterMap());
		//权限控制 数据筛选
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if (oConvertUtils.isNotEmpty(loginUser.getUserType()) && 1 != loginUser.getUserType()) {
			if (2 == loginUser.getUserType()) {

				//分站
				String siteId = loginUser.getSiteId();
				//获取商户ids
				List<String> merchantIds = commonAPI.findMerchantForSite(siteId);
				queryWrapper.in(!merchantIds.isEmpty(), "merchant_id", merchantIds);

			} else if (3 == loginUser.getUserType()) {
				//商户
				String merchantId = loginUser.getMerchantId();
				//获取门店
				if (oConvertUtils.isNotEmpty(loginUser.getCurrentSelectedShopId())) {
					queryWrapper.eq("shop_id", loginUser.getCurrentSelectedShopId());
				} else {
					queryWrapper.eq("merchant_id", merchantId);
				}

			}
//            queryWrapper.eq("status",1);
		}

		Page<OrderRefund> page = new Page<OrderRefund>(pageNo, pageSize);
		IPage<OrderRefund> pageList = orderRefundService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 强制退款订单-商家端
	 *
	 * @param orderNos 订单号集合
	 */
	@AutoLog(value = "强制退款订单-商家端")
	@ApiOperation(value = "StringUtils.isBlank(订单-商家端", notes = "params: refundId")
	@PutMapping("/forceOrderRefund")
	public Result<?> forceOrderRefund(@RequestParam List<String> orderNos, @RequestParam("remark") String refundCause) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		for (String orderNo : orderNos) {
			orderRefundService.forceOrderRefund(orderNo, refundCause, loginUser.getUsername(), loginUser.getUserType());
		}
		return Result.OK();
	}

	/**
	 * 强制订单完成-商家端
	 *
	 * @param orderNos 订单号集合
	 */
	@AutoLog(value = "强制	完成订单-商家端")
	@ApiOperation(value = "强制完成订单-商家端")
	@PutMapping("/forceOrderComplete")
	public Result<Integer> forceOrderComplete(@RequestParam List<String> orderNos, @RequestParam("remark") String completeCause) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		for (String orderNo : orderNos) {
			orderRefundService.forceOrderComplete(orderNo, completeCause, loginUser.getUsername(), loginUser.getUserType());
		}
		return Result.OK(1);
	}

	/**
	 *   添加
	 *
	 * @param orderRefund
	 * @return
	 */
//	@AutoLog(value = "订单退款信息-添加")
//	@ApiOperation(value="订单退款信息-添加", notes="订单退款信息-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody OrderRefund orderRefund) {
//		orderRefundService.save(orderRefund);
//		return Result.OK("添加成功！");
//	}

	/**
	 *  编辑
	 *
	 * @param orderRefund
	 * @return
	 */
	@AutoLog(value = "订单退款信息-编辑")
	@ApiOperation(value="订单退款信息-编辑", notes="订单退款信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody OrderRefund orderRefund) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		return orderRefundService.edit(orderRefund,loginUser.getUsername());
	}

	/**
	 *  退款审核
	 *
	 * @param orderRefund
	 * @return
	 */
	@AutoLog(value = "退款审核")
	@ApiOperation(value="退款审核", notes="退款审核 id audit")
	@PostMapping(value = "/auditRefundOrder")
	public Result<?> auditRefundOrder(@RequestBody OrderRefund orderRefund) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if (oConvertUtils.isEmpty(orderRefund.getAudit()) && oConvertUtils.isEmpty(orderRefund.getId())){
			return Result.error("参数错误");
		}
		OrderRefund orderRefund1 = orderRefundService.getById(orderRefund.getId());
		if (oConvertUtils.isEmpty(orderRefund1)){
			return Result.error("参数错误，订单不存在");
		}
		if (orderRefund1.getAudit()==1){
			return Result.error("该退款单已经审核过咯，请勿重复操作");
		}
		orderRefund1.setAudit(orderRefund.getAudit());
		return orderRefundService.edit(orderRefund1,loginUser.getUsername());
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "订单退款信息-通过id删除")
	@ApiOperation(value="订单退款信息-通过id删除", notes="订单退款信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		orderRefundService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "订单退款信息-批量删除")
	@ApiOperation(value="订单退款信息-批量删除", notes="订单退款信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.orderRefundService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "订单退款信息-通过id查询")
	@ApiOperation(value="订单退款信息-通过id查询", notes="订单退款信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		OrderRefund orderRefund = orderRefundService.getById(id);
		if(orderRefund==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(orderRefund);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param orderRefund
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OrderRefund orderRefund) {
        return super.exportXls(request, orderRefund, OrderRefund.class, "订单退款信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OrderRefund.class);
    }

}
